package com.bank.abmock.Utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mifmif.common.regex.Generex;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Iterator;
import java.util.Map;

public class JsonUtils {
    public static JsonNode generateValues(JsonNode root) {
        Iterator<String> iterator = root.fieldNames();
        for (JsonNode node : root) {
            String nodeName = iterator.next();
            if (node.getNodeType().equals(JsonNodeType.STRING)) {
                String rule = node.textValue();
                if(rule.startsWith("regex")){
                    String regex = org.apache.commons.lang3.StringUtils.substringAfter(rule,"regex");
                    Generex generex = new Generex(regex);
                    String randomStr = generex.random();
                    ((ObjectNode) root).put(nodeName, randomStr);
                }else{
                    ((ObjectNode) root).put(nodeName, RandomStringUtils.random(5, 0x3130, 0x9fa5, false, false));
                }
            }
            if (node.getNodeType().equals(JsonNodeType.NUMBER)) {
                ((ObjectNode) root).put(nodeName, Double.parseDouble(RandomStringUtils.randomNumeric(2)));
            }
            if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
                generateValues(node);
            }
            if (node.getNodeType().equals(JsonNodeType.ARRAY)) {
                Iterator<JsonNode> arrIter = ((ArrayNode) node).elements();
                while (arrIter.hasNext()) {
                    generateValues(arrIter.next());
                }
            }
        }
        return root;
    }

    public static JsonNode generateValues(JsonNode root, Map<String, Object> targetParamMap) {

        Iterator<String> iterator = root.fieldNames();
        for (JsonNode node : root) {
            String nodeName = iterator.next();
            if (node.getNodeType().equals(JsonNodeType.STRING)) {
                if (targetParamMap.containsKey(nodeName)) {
                    ((ObjectNode) root).put(nodeName, (String) targetParamMap.get(nodeName));
                } else {
                    ((ObjectNode) root).put(nodeName, RandomStringUtils.random(5, 0x3130, 0x9fa5, false, false));
                }

            }
            if (node.getNodeType().equals(JsonNodeType.NUMBER)) {
                if (targetParamMap.containsKey(nodeName)) {
                    ((ObjectNode) root).put(nodeName, (Double) targetParamMap.get(nodeName));
                } else {
                    ((ObjectNode) root).put(nodeName, Double.parseDouble(RandomStringUtils.randomNumeric(2)));
                }
            }
            if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
                generateValues(node,targetParamMap);
            }
            if (node.getNodeType().equals(JsonNodeType.ARRAY)) {
                Iterator<JsonNode> arrIter = ((ArrayNode) node).elements();
                while (arrIter.hasNext()) {
                    generateValues(arrIter.next(),targetParamMap);
                }
            }
        }
        return root;
    }
}
